import json
from registry.forward import Forward


class v2:
    def __init__(self, forward: Forward) -> None:
        self.forward = forward
        pass

    def _catalog(self) -> dict:
        """
        获取仓库列表
        """
        result = self.__get("/v2/_catalog")
        return json.loads(result.text)

    def tags(self, repository: str) -> dict:
        """
        通过仓库获取标签
        """
        result = self.__get(f"/v2/{repository}/tags/list")
        return json.loads(result.text)

    def manifest(self, repository: str, reference: str) -> dict:
        """
        通过引用获取清单
        """
        result = self.__get(f"/v2/{repository}/manifests/{reference}")
        return json.loads(result.text)

    def delete(self, repository: str, reference: str) -> dict:
        """
        通过引用删除清单
        """
        print(f"删除清单: {repository}:{reference}")
        result = self.__delete(f"/v2/{repository}/manifests/{reference}")
        return json.loads(result.text)

    def __get(self, context: str):
        """
        获取指定路径的内容
        """
        return self.forward.get(context)

    def __delete(self, context: str):
        """
        删除指定路径的内容
        """
        return self.forward.delete(context)
